package com.ayf.payment.game.api.service.platform.impl;

import com.ayf.payment.game.api.dto.platform.MerchantSettleRateConfigDTO;
import com.ayf.payment.game.api.dto.platform.MerchantSettleRateDTO;
import com.ayf.payment.game.api.dto.platform.MerchantSettleRateDTO2;
import com.ayf.payment.game.api.dto.platform.PaymentProductAndRateDTO;
import com.ayf.payment.game.api.entity.platform.MerchantSettleRate;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.MerchantConfigMapper;
import com.ayf.payment.game.api.mapper.platform.MerchantSettleRateMapper;
import com.ayf.payment.game.api.service.platform.MerchantSettleRateConfigService;
import com.ayf.payment.game.api.service.platform.MerchantSettleRateService;
import com.ayf.payment.game.api.service.platform.PaymentProductService;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MerchantSettleRateServiceImpl implements MerchantSettleRateService {

    @Resource
    private MerchantSettleRateMapper merchantSettleRateMapper;

    @Resource
    private MerchantSettleRateConfigService merchantSettleRateConfigService;

    @Resource
    private PaymentProductService paymentProductService;

    @Resource
    private MerchantConfigMapper merchantConfigMapper;

    @Resource
    private com.ayf.payment.game.api.config.SystemInitialization SystemInitialization;

    @Override
    public void insertMerchantSettleRate(MerchantSettleRateDTO merchantSettleRateDTO) {
        MerchantSettleRate merchantSettleRate=new MerchantSettleRate();
        merchantSettleRate.setIdentification(merchantSettleRateDTO.getIdentification());
        merchantSettleRate.setCreateTime(new Date());
        merchantSettleRate.setIsAgent(merchantSettleRateDTO.getIsAgent());
        merchantSettleRate.setIsFirst(merchantSettleRateDTO.getIsFirst());
        merchantSettleRateMapper.insert(merchantSettleRate);
        for(MerchantSettleRateConfigDTO mrcd:merchantSettleRateDTO.getConfigList()){
            PaymentProductAndRateDTO prDto= paymentProductService.getPaymentProductAndRateDTO(mrcd.getPayProductId());
            if(mrcd.getPaymentRate().compareTo(new BigDecimal(prDto.getPaymentRate()))==1) {
                throw new ParameterException("费率不能高于通道费率(" + prDto.getPaymentRate() + ")！");
            }
        }
        merchantSettleRateConfigService.insertMerchantSettleRateConfigs(merchantSettleRateDTO.getConfigList(),merchantSettleRate.getId());
        SystemInitialization.InitializationData("商户结算更新费率");
    }

    @Override
    @Transactional
    public void updateMerchantSettleRate(MerchantSettleRateDTO merchantSettleRateDTO) {
        MerchantSettleRate merchantSettleRate=new MerchantSettleRate();
        merchantSettleRate.setId(merchantSettleRateDTO.getId());
        merchantSettleRate.setIdentification(merchantSettleRateDTO.getIdentification());
        merchantSettleRate.setIsAgent(merchantSettleRateDTO.getIsAgent());
        merchantSettleRate.setIsFirst(merchantSettleRateDTO.getIsFirst());
        merchantSettleRateMapper.updateByPrimaryKeySelective(merchantSettleRate);
        Map<Integer,Map<Integer,BigDecimal>> map=merchantSettleRateMapper.getProxyLowerMerchantMaxSettleRateMapByPlatformRateId(merchantSettleRateDTO.getId());
        List<MerchantSettleRateConfigDTO> list= merchantSettleRateDTO.getConfigList();
        for(MerchantSettleRateConfigDTO mrcd:list){
            PaymentProductAndRateDTO prDto= paymentProductService.getPaymentProductAndRateDTO(mrcd.getPayProductId());
            if(mrcd.getPaymentRate().compareTo(new BigDecimal(prDto.getPaymentRate()))==1) {
                throw new ParameterException(prDto.getAliasName()+"费率不能高于通道("+prDto.getPaymentRate()+")费率！");
            }
            if(!map.isEmpty() && null !=map.get(mrcd.getPayProductId()) && mrcd.getPaymentRate().compareTo(map.get(mrcd.getPayProductId()).get("paymentRate")) <0  ){
                throw new ParameterException(map.get(mrcd.getPayProductId()).get("payProductName")+"费率不能低于代理下属商户最大费率["+map.get(mrcd.getPayProductId()).get("paymentRate")+"]");
            }
            merchantSettleRateConfigService.updateMerchantSettleRateConfig(mrcd);
        }
        SystemInitialization.InitializationData("更新商户结算产品费率" );
    }

    @Override
    public void delMerchantSettleRate(Integer rateId) {
        List<Integer> ids = merchantConfigMapper.selectIdsByIdentificationId(rateId);
        if (ids != null && ids.size() > 0) {
            throw new ParameterException("有商户正在使用该费率，请调整为其他费率后再操作");
        }
        merchantSettleRateConfigService.delMerchantSettleRateConfigs(rateId);
        merchantSettleRateMapper.deleteByPrimaryKey(rateId);
        SystemInitialization.InitializationData("删除商户结算费率");
    }

    @Override
    public List<MerchantSettleRateDTO> selectAll(Integer rateId) {
        return merchantSettleRateMapper.selectMerchantSettleRateAll(rateId);
    }

    @Override
    public List<MerchantSettleRateDTO2> selectAll2() {
        return merchantSettleRateMapper.selectMerchantSettleRateAll2();
    }

    /**
     *
     * @author lch
     * @date 2022/3/7
     */
	@Override
	public MerchantSettleRateDTO getById(Integer id) {
        MerchantSettleRate merchantSettleRate = this.merchantSettleRateMapper.selectByPrimaryKey(id);
        return BeanConvertUtils.convert(merchantSettleRate, MerchantSettleRateDTO.class);
	}
}
